package eclipse.lib.scripting;
import javax.script.*;

import java.io.*;
import java.net.*;
import eclipse.*;
import eclipse.resources.*;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class JScript {
	private static final ScriptEngineManager mgr 		= new ScriptEngineManager();
	private static final ScriptEngine jsEngine	 		= mgr.getEngineByName("JavaScript");
	private static final Invocable invocableEngine	 	= (Invocable) jsEngine;
	
	/**
	 * Creates an object in the Scripting engine
	 * @param alias Name of the object
	 * @param obj The Object that is added
	 */
	public static final void makeObject(String alias, Object obj)
	{
		jsEngine.put(alias, obj);		
	}	
	
	/**
	 * Executes code from the Scripting engine
	 * @param name Name of the script
	 * @throws ScriptException 
	 */
	public static final Object run(String name) throws ScriptException
	{		
			return jsEngine.eval(name);		
	}
	/**
	 * Evaluates a script
	 * @param name Name of the function
	 * @param args Arguments of the function
	 */
	public static final Object evaluate(String name, Object... args)
	{
		try
		{
			return invocableEngine.invokeFunction(name, args);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}		
		return null;
	}
	/**
	 * Adds a Script from the jar
	 * @param Script Script to add
	 */
	public static final void addScriptFromJar(String Script)
	{
		importScript(ResourceManager.getInputStream(Script));
		
	}
	/**
	 * Adds a script from a Stream
	 * @param script Script to add
	 */
	public static final void importScript(InputStream script)
	{
		try
		{
			final InputStreamReader IS = new InputStreamReader(script);
			jsEngine.eval(IS);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Adds a script from a URL
	 * @param script Script to add
	 */
	public static final void addScriptFromURL(URL script)
	{		
		try
		{
			importScript(script.openStream());
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}		
	}
	/**
	 * Adds a script from a String path
	 * @param script Script to add
	 */
	public static final void addScriptFromPath(String script)
	{
		script = "Scripts/" + script;
		try 
		{
			final Reader reader = new FileReader(script);
		    jsEngine.eval(reader);
		} 
		catch(Exception e) 
		{
			ErrorManager.handleError(e);
		}	
	}
	/**
	 * Adds a procedure to the engine	 
	 * @param Procedure Procedure to add as a String
	 */
	public static final void addProcedure(String Procedure)
	{
		try
		{
			jsEngine.eval(Procedure);		
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
}
